package com.lemon.core.feign.config;

import com.lemon.core.feign.interceptor.FeignInterceptor;
import feign.Logger;
import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

/**
 * Feign配置
 *
 * @author pan
 * @link https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#feign-capability-support
 * @since 2019-07-16
 */
@Slf4j
public class FeignConfiguration {

    /**
     * feign请求拦截器
     *
     * @return RequestInterceptor
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return new FeignInterceptor();
    }


    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    /**
     * RestTemplate请求不支持content type [text/html;charset=UTF-8]类型，需要手动补充text/html格式
     *
     * @return RestTemplate
     */
    @Bean()
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
            MediaType.TEXT_HTML,
            MediaType.TEXT_PLAIN));
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        return restTemplate;
    }

}
